/*
 *  Copyright 2011 yura.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */

package org.opu.vcforms.bean;

import java.awt.event.ActionListener;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;
import javax.swing.Action;
import org.opu.vcforms.actions.About;
import org.opu.vcforms.actions.AbstractAction;
import org.opu.vcforms.actions.DbConnect;
import org.opu.vcforms.actions.Exit;
import org.opu.vcforms.actions.Help;
import org.opu.vcforms.actions.Statement;
import org.opu.vcforms.actions.Tools;

/**
 *
 * @author yura
 */
public class EventBus {

    private ApplicationData data;
    private Map<String, AbstractAction> actions;

    /** Actions thet fires when application closed */
    public final Set<ActionListener> listeners;

    /**
     *  Create new instance
     * @param data data
     */
    public EventBus(ApplicationData data) {
        this.data = data;
        actions = new HashMap<String, AbstractAction>();
        listeners = new LinkedHashSet<ActionListener>();
        addAction(new Statement(data), actions);
        addAction(new Exit(listeners), actions);
        addAction(new Tools(), actions);
        addAction(new DbConnect(data), actions);
        addAction(new About(), actions);
        addAction(new Help(), actions);
    }

    /**
     *  Return create statement action.
     * It`s main action off applicationg, it`s
     * create statement by data from source
     * @return create statement action.
     */
    public Action getCreateStatementAction(){
        return actions.get(Statement.MNAME);
    }

    /**
     *  Return exit action
     * @return exit action
     */
    public Action getExitAction(){
        return actions.get(Exit.MNAME);
    }

    /**
     *  Return correcte (tools) action.
     * Tools action containe dialog with tabbet pane
     * where collected all posible customizes.
     * @return correcte (tools) action.
     */
    public Action getToolsAction(){
        return actions.get(Tools.MNAME);
    }

    /**
     *  Return about Action
     * @return about Action
     */
    public Action getAboutAction(){
        return actions.get(About.MNAME);
    }

    /**
     *  Return help action
     * @return help action
     */
    public Action getHelpAction(){
        return actions.get(Help.MNAME);
    }

    /**
     *  Return database connect action.
     * Database connect can correct int application
     * correct action, but it`s can be correct
     * in self (for example when application startup)
     * @return database connect action
     */
    public Action getDbConnectAction(){
        return actions.get(DbConnect.MNAME);
    }

    /**
     *  Return applicationg main model
     * @return main applicationg model
     */
    public ApplicationData getData() {
        return data;
    }

    /**
     *  Return action by action name
     * @param name action name
     * @return action by action name
     */
    public AbstractAction getAction(String name) {
        return actions.get(name);
    }

    private void addAction(AbstractAction action, Map actions){
        actions.put(action.getName(), action);
    }
}
